package com.itheima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.BrandMapper;
import com.itheima.mall.pojo.Brand;
import com.itheima.mall.service.PmsBrandService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 添加标签
     * @param pmsBrand
     */
    @Override
    public void create(Brand pmsBrand) {
        brandMapper.insert(pmsBrand);

    }

    /**
     * 通过id删除品牌
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteById(id);
    }

    /**
     * 批量删除皮牌
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(ids));
        brandMapper.deleteBatchIds(list);
    }

    //分页显示品牌
    @Override
    public PageResult brandList(String keyword, Integer pageNum, Integer pageSize) {
        Page<Brand> brandPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据条件模糊查询品牌
        lambdaQueryWrapper.like(keyword != null , Brand::getName , keyword);
        Page<Brand> pageInfo = brandMapper.selectPage(brandPage, lambdaQueryWrapper);
        return new PageResult(pageNum , pageSize , (int) pageInfo.getTotal() , pageInfo.getRecords());
    }

    //获取所有品牌标签
    @Override
    public List<Brand> brandListAll() {
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Brand> list = brandMapper.selectList(lambdaQueryWrapper);
        return list;
    }

    //批量更新厂家制造商状态
    @Override
    public void updateFactoryStatusByIds(Integer factoryStatus, Integer[] ids) {
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Brand::getId , ids);
        Brand brand = new Brand();
        brand.setFactoryStatus(factoryStatus);

        brandMapper.update(brand , lambdaQueryWrapper);

    }

    //批量更新更新显示状态
    @Override
    public void updateShowStatus(Integer[] ids, Integer showStatus) {

        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Brand::getId , ids);
        Brand brand = new Brand();
        brand.setShowStatus(showStatus);
        brandMapper.update(brand , lambdaQueryWrapper);
    }
    //更新品牌
    @Override
    public void updateBrandById(Integer id, Brand pmsBrandParam) {
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Brand::getId , id);
        brandMapper.update(pmsBrandParam , lambdaQueryWrapper);
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @Override
    public Brand selectById(Integer id) {
        Brand brand = brandMapper.selectById(id);
        return brand;
    }


}
